package com.hotel.service;

import com.hotel.mapper.HotelMapper;
import com.hotel.mapper.RoomMapper;
import com.hotel.mapper.StaffMapper;
import com.hotel.pojo.Hotel;
import com.hotel.pojo.Room;
import com.hotel.pojo.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class StaffService {

    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private RoomMapper roomMapper;

    // 检验酒店员工账号密码
    public boolean checkStaff(Staff staff) {
        List<Staff> staffList = staffMapper.findAllStaff();
        List<Staff> collect = staffList.stream().filter(staff1 -> staff.getUsername().equals(staff1.getUsername())
                && staff.getPassword().equals(staff1.getPassword())).collect(Collectors.toList());
        return !collect.isEmpty();
    }

    // 登录
    public void loginStaff(String name) {
        staffMapper.loginStaff(name);
    }

    // 注销
    public void exitStaff(String name) {
        staffMapper.exitStaff(name);
    }

    // 检查是否登录
    public boolean isLogin(String name) {
        return staffMapper.isLogin(name) == 1;
    }

    // 查看所有员工
    public List<Staff> findAllStaff() {
        return staffMapper.findAllStaff();
    }

    // 更新酒店员工信息
    public void updateStaff(Staff staff) {
        staffMapper.updateStaff(staff);
    }

    // 查找酒店员工是否存在
    public boolean findStaffByName(String name) {
        return staffMapper.findStaffByName(name) != null;
    }

    // 查看酒店负责人
    public String findStaffByHotel(String hotelName) {
        return hotelMapper.findStaffByHotel(hotelName);
    }

    // 添加酒店
    public boolean addHotel(Hotel hotel) {
        Hotel hotelByName = hotelMapper.findHotelByName(hotel.getName());
        // 酒店已经存在
        if (hotelByName != null) {
            return false;
        } else {
            hotelMapper.addHotel(hotel);
            return true;
        }
    }

    // 修改酒店
    public void updateHotel(Hotel hotel) {
        hotelMapper.updateHotel(hotel);
    }

    // 查看所有酒店
    public List<Hotel> findAllHotel() {
        List<Hotel> hotelList = hotelMapper.findAllHotel();
        for(Hotel hotel : hotelList) {
            List<Room> roomList = roomMapper.findRoomByHotel(hotel.getName());
            hotel.setRoomList(roomList);
        }
        return hotelList;
    }

    // 查看所负责的酒店
    public List<Hotel> findHotelByStaff(String staffName) {
        List<Hotel> hotelList = hotelMapper.findHotelByStaff(staffName);
        for(Hotel hotel : hotelList) {
            List<Room> roomList = roomMapper.findRoomByHotel(hotel.getName());
            hotel.setRoomList(roomList);
        }
        return hotelList;
    }

    // 删除酒店
    public void deleteHotel(Hotel hotel) {
        hotelMapper.deleteHotel(hotel);
    }

    // 添加客房信息
    public boolean addRoom(Room room) {
        if (roomMapper.findRoomByHotelAndType(room) == null) {
            roomMapper.addRoom(room);
            return true;
        } else {
            return false;
        }
    }

    // 修改客房信息
    public void updateRoom(Room room) {
        roomMapper.updateRoom(room);
    }

    // 查看所有客房信息
    public List<Room> findAllRoom() {
        return roomMapper.findAllRoom();
    }

    // 删除客房
    public void deleteRoom(Room room) {
        roomMapper.deleteRoom(room);
    }
}
